<?php
/**
 * 模板消息
 */

class TplMsg extends Wechat{

    // 模板消息uri设置
    private $http_path  = 'https://api.weixin.qq.com/cgi-bin/template/TPL_OP?access_token=ACCESS_TOKEN';
    // 模板消息发送uri设置
    private $http_send_path  = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN';

    /**
     * 构造方法，调用微信高级接口时实例化SDKt
     */
    public function __construct($appid, $secret){
        parent::__construct($appid, $secret);
        $token = $this->getAccessToken();
        $this->http_path = str_replace('ACCESS_TOKEN', $token, $this->http_path);
        $this->http_send_path = str_replace('ACCESS_TOKEN', $token, $this->http_send_path);
    }

    /**
     * 设置所属行业
     * @param  string $industry_id1 公众号模板消息所属行业编号
     * @param  string $industry_id2 公众号模板消息所属行业编号
     */
    public function set_industry($industry_id1, $industry_id2){
        $this->http_path = str_replace('TPL_OP', 'api_set_industry', $this->http_path);
        $data = array(
            'industry_id1' => $industry_id1,
            'industry_id2' => $industry_id2
        );

        $data = $this->http($this->http_path, null, $data, 'POST');
        return json_decode($data, true);
    }

    /**
     * 获取设置的行业信息
     */
    public function get_industry($media_id){
        $this->http_path = str_replace('TPL_OP', 'get_industry', $this->http_path);
        $data = $this->http($this->http_path, null);
        
        return json_decode($data, true);
    }

    /**
     * 获得模板ID
     * @param  string $industry_id2 模板库中模板的编号，有“TM**”和“OPENTMTM**”等形式
     */
    public function add_template($tpl_id_short){
        $this->http_path = str_replace('TPL_OP', 'api_add_template', $this->http_path);
        $data = array(
            'template_id_short' => $tpl_id_short,
        );

        $data = $this->http($this->http_path, null, $data, 'POST');
        return json_decode($data, true);
    }

    /**
     * 获取模板列表
     */
    public function get_all_private_template($tpl_id_short){
        $this->http_path = str_replace('TPL_OP', 'get_all_private_template', $this->http_path);
        $data = $this->http($this->http_path, null);

        return json_decode($data, true);
    }

    /**
     * 删除模板
     * @param  string $industry_id2 模板库中模板的编号，有“TM**”和“OPENTMTM**”等形式
     */
    public function del_private_template($template_id){
        $this->http_path = str_replace('TPL_OP', 'del_private_template', $this->http_path);
        $data = array(
            'template_id' => $template_id,
        );

        $data = $this->http($this->http_path, null, $data, 'POST');
        return json_decode($data, true);
    }

    /**
     * 发送模板消息
     * @param  string $industry_id2 模板库中模板的编号，有“TM**”和“OPENTMTM**”等形式
     */
    public function tpl_msg_send($data){
        $data = $this->http($this->http_send_path, null, $data, 'POST');
        return json_decode($data, true);
    }

}